<?php
/**
 * Created by PhpStorm.
 * User: cqfut
 * Date: 2018/11/1
 * Time: 15:45
 */
require_once (ROOTDIR."/config/db_config.php");

class DbUtil
{
    static private $_instance=null;

    private $dbh = null;

    private function __construct(){
        $dsn = "mysql:host=".DbConfig::$DB_HOST.";";
        $dsn .= "port=".DbConfig::$DB_PORT.";";
        $dsn .= "dbname=".DbConfig::$DB_NAME;
        try {
            $this->dbh = new PDO($dsn, DbConfig::$DB_USER, DbConfig::$DB_PASS,
                array(PDO::MYSQL_ATTR_INIT_COMMAND => "set names utf8"));
        }catch (Exception $e) {
            echo '数据库连接失败';
        }
    }

    //用于访问类的实例的公共的静态方法
    static public function getInstance(){
        if( !(self::$_instance instanceof DbUtil) ){
            self::$_instance = new self;
        }
        return self::$_instance;
    }

    // 产生where子句
    static public function genWhereSql( $sqlWhere, $keyName )
    {
        if ( empty($sqlWhere) ) {
            $sqlWhere .= " where `".$keyName."`= ?";
        }
        else {
            $sqlWhere .= " and `".$keyName."`= ?";
        }
        return $sqlWhere;
    }

    static public function genSelectSql($colNames, $tableName) {
        $sql = "select ";
        foreach ( $colNames as $colName ) {
            $sql .= "`".$colName."`,";
        }
        $sql = substr($sql, 0, strlen($sql) - 1 );
        $sql .= "  from ".$tableName;
        return $sql;
    }

    static public function genUpdateSql($colNames, $tableName) {
        $sql = "update ".$tableName." set ";
        foreach ( $colNames as $colName ) {
            $sql .= "`".$colName."` = ?,";
        }
        $sql = substr($sql, 0, strlen($sql) - 1 );
        return $sql;
    }

    static public function genDelSql($colNames, $tableName) {
        $sql = "delete from ".$tableName." where ";
        foreach ( $colNames as $colName ) {
            $sql .= " `".$colName."` = ? and";
        }
        $sql = substr($sql, 0, strlen($sql) - 3 );
        return $sql;
    }

    static public function genInsertSql($colNames, $tableName) {
        $sql = "insert into ".$tableName."(";
        $sqlVal = ") VALUES(";
        foreach ( $colNames as $colName ) {
            $sql .= "`".$colName."`,";
            $sqlVal .= "?,";
        }

        $sql = substr($sql, 0, strlen($sql) - 1 );
        $sqlVal = substr($sqlVal, 0, strlen($sqlVal) - 1 );
        $sql .= $sqlVal.")";
        return $sql;
    }

    public function query($sql){
        $num_args = func_num_args();
        $args = func_get_args();
        $args=array_slice($args,1,--$num_args);
        return  $this->query2($sql, $args);
    }

    // args为参数数组
    function query2($sql, $args){
        $sth = $this->dbh->prepare($sql);
        $sth->execute($args);
        $result=null;
        if(stripos($sql,"select") === 0){
            $result=$sth->fetchAll();
        }else if(stripos($sql,"insert") === 0){
            $result=$this->dbh->lastInsertId();
        }else{
            $result=$sth->rowCount();
        }
        $sth->closeCursor();
        return $result;
    }
}